package com.icim.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import com.icim.twqc.importer.entity.TwqcMfgFileXmlEntity;
import com.icim.twqc.importer.entity.TwqcMfgInfoXmlEntity;
import com.icim.twyd.exporter.xmlentity.TwydBoxXmlEntityOK;
import com.icim.twyd.exporter.xmlentity.TwydCellXmlEntityOK;
import com.icim.twyd.exporter.xmlentity.TwydMessageXmlEntitySC;
import com.icim.twyd.importer.xmlentity.TwydBoxXmlEntity;
import com.icim.twyd.importer.xmlentity.TwydCellXmlEntity;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlConvertUtil {

    public static final String XML_HEADER = "<?xml version='1.0' encoding='UTF-8' ?>";

    public static String obj2Xml(Object obj, String rootElement) {
        StringBuffer buffer = new StringBuffer(XML_HEADER);
        buffer.append("\n");
        XStream xstream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
        xstream.alias(rootElement, obj.getClass());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.autodetectAnnotations(true);
        buffer.append(xstream.toXML(obj));
        return buffer.toString();
    }
    //新建针对232客户的回传xml
    public static String obj2XmlFor232(Object obj, String rootElement) {
        StringBuffer buffer = new StringBuffer(XML_HEADER);
        buffer.append("\n");
        XStream xstream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
        xstream.alias(rootElement, obj.getClass());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.autodetectAnnotations(true);
        
        xstream.addImplicitCollection(TwqcMfgFileXmlEntity.class, "MFGINFO");
        buffer.append(xstream.toXML(obj));
        return buffer.toString();
      }
    public static String obj2Xml1(Object obj, String rootElement) {
      StringBuffer buffer = new StringBuffer(XML_HEADER);
      buffer.append("\n");
      XStream xstream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
      xstream.alias(rootElement, obj.getClass());
      xstream.setMode(XStream.NO_REFERENCES);
      xstream.autodetectAnnotations(true);
      
      xstream.addImplicitCollection(TwydBoxXmlEntityOK.class, "CELL");
      buffer.append(xstream.toXML(obj));
      return buffer.toString();
    }
    
    public static String obj2Xml2(Object obj, String rootElement) {
      StringBuffer buffer = new StringBuffer(XML_HEADER);
      buffer.append("\n");
      XStream xstream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
      xstream.alias(rootElement, obj.getClass());
      xstream.setMode(XStream.NO_REFERENCES);
      xstream.autodetectAnnotations(true);
      
      xstream.addImplicitCollection(TwydMessageXmlEntitySC.class, "CELL");
      buffer.append(xstream.toXML(obj));
      return buffer.toString();
    }

    public static Object xml2Obj(String xmlStr, Class clazz) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(clazz);
        xstream.registerConverter(new DoubleConverter());
        xstream.registerConverter(new IntConverter());
        return xstream.fromXML(xmlStr);
    }

//    public static Object file2Object(String fileName, String rootNodeName, Class clazz) {
//        String fileContent = file2String(fileName);
//        return xml2Obj(fileContent, rootNodeName, clazz);
//    }

    /**
     * 将XML文件转换成 对象
     * 
     * @param fileName
     * @param clazz
     * @return
     */
    public static Object file2Object(File file, Class clazz) {
        String fileContent = file2tring(file);
        return xml2Obj(fileContent, clazz);
    }
    
    /**
     * 将XML文件转换成 对象
     * 
     * @param fileName
     * @param clazz
     * @return
     */
    public static Object file2Object1(File file, Class clazz) {
        String fileContent = file2tring(file);
        return xml2Obj1(fileContent, clazz);
    }
    
    public static Object xml2Obj1(String xmlStr, Class clazz) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(clazz);
        xstream.registerConverter(new DoubleConverter());
        xstream.registerConverter(new IntConverter());
        xstream.alias("CELL",TwydCellXmlEntity.class);  
        xstream.addImplicitCollection(TwydBoxXmlEntity.class, "CELL");//隐式集合 隐藏lovers 
        return xstream.fromXML(xmlStr);
    }
    
    //新建针对twqc Xml的解析方法
    public static Object file2ObjectForTwqc(File file, Class clazz) {
        String fileContent = file2tring(file);
        return xml2ObjForTwqc(fileContent, clazz);
    }
    public static Object xml2ObjForTwqc(String xmlStr, Class clazz) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(clazz);
        xstream.registerConverter(new DoubleConverter());
        xstream.registerConverter(new IntConverter());
        xstream.alias("MFGINFO",TwqcMfgInfoXmlEntity.class);
        xstream.addImplicitCollection(TwqcMfgFileXmlEntity.class, "MFGINFO");
        return xstream.fromXML(xmlStr);
    }

    private static String file2tring(File file) {
        String encoding = "UTF-8";
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }
}
